/*
 * @Author: yangrongxin
 * @Date: 2021-08-04 14:11:41
 * @LastEditors: guoju
 * @LastEditTime: 2022-01-07 11:28:05
 */
// import { useState } from 'react';
import { View, Text } from '@tarojs/components';
import { AtIcon } from 'taro-ui';
import Taro from '@tarojs/taro';
import { RenderItemComponent } from '@/pages/PatientEducation/components/index';
import { CHANNEL_TYPE, CHANNEL_TYPE_VALUE_ENUM } from '@/pages/My/compoents/MyAssessment/dataDictionary';
import { getConfirmInfoFlag } from '@/pages/AssessmentDetail/service';
import { EvaluationItem, SIZE_TYPE } from '../../../Evaluation/components';
import styles from './index.module.less';

const WaitTextItem = (props: any) => {
  const { name, measureTableId, measureTableRecordId, evaluationId, evaluationType, questionNums, updateSetUserData } =
    props;
  const isNotTeam = evaluationType !== CHANNEL_TYPE.ONLINE; // 用于团队和院内

  // 跳转到测试的页面
  const toTest = (isValidPassword: boolean = false) => {
    console.log('isValidPassword', isValidPassword);
    // 当前是团队和院内的消息 需要进行判断
    if (isNotTeam) {
      if (evaluationType === CHANNEL_TYPE.THECOURT && !isValidPassword) {
        updateSetUserData({
          isShowUserData: true,
          measureTableId,
          name,
          measureTableRecordId,
          evaluationId, // 当前对应的测评id
          evaluationType, // 当前对应的测评类型
          hideValidPassWord: false, // 当前是验证密码
          validConfirmInfoFn: toTest, // 判断当前是不是要进行消息确认
        });
        return;
      }
      // 判断消息提示 之后进行跳转
      getConfirmInfoFlag().then((isFirstLogin: any) => {
        console.log('isFirstLogin', isFirstLogin);
        // 1 已经确认过 2 未确认过
        if (isFirstLogin?.confirmInfoFlag === 1) {
          updateSetUserData({
            isShowUserData: false,
          });
          Taro.navigateTo({
            url: `/EvaluationModule/pages/CheckboxAnwser/index?measureTableId=${measureTableId}&title=${name}&measureTableRecordId=${measureTableRecordId}&evaluationId=${evaluationId}&evaluationType=${evaluationType}`,
          });
        }
        // 当前是院内测评 需要每次都重新验证密码

        // 当前未确认过 展示弹窗
        if (isFirstLogin?.confirmInfoFlag === 2) {
          updateSetUserData({
            isShowUserData: true,
            measureTableId,
            name,
            measureTableRecordId,
            evaluationId, // 当前对应的测评id
            evaluationType, // 当前对应的测评类型
            hideValidPassWord: true, // 当前是验证密码
            validConfirmInfoFn: toTest, // 判断当前是不是要进行消息确认
          });
        }
      });
    } else {
      // 线上测评直接进行跳转
      Taro.navigateTo({
        url: `/EvaluationModule/pages/CheckboxAnwser/index?measureTableId=${measureTableId}&title=${name}&measureTableRecordId=${measureTableRecordId}&evaluationId=${evaluationId}&evaluationType=${evaluationType}`,
      });
    }
  };

  return (
    <View className={styles.waitTextItem} onClick={() => toTest(false)}>
      <View>
        <Text className="ellipsisText">{name || '这是展示的默认名称'}</Text>
        {/* <Text>{dayjs(xCreateTime).format('MM月DD日')}</Text> */}
      </View>
      <View>
        <Text>共{questionNums}题</Text>
        <View>
          <Text>{CHANNEL_TYPE_VALUE_ENUM[evaluationType]}</Text>
        </View>
      </View>
    </View>
  );
};

const RecommandTest = (props: {
  measureTableList: any[];
  waitSuccessEvaluation: number;
  waitMeasureTableList: any;
  articleList: any[];
  updateSetUserData: any; // 控制当前展示用户信息修改的弹窗
}) => {
  const {
    articleList = [],
    measureTableList = [],
    waitMeasureTableList = [],
    waitSuccessEvaluation = 0,
    updateSetUserData,
  } = props;
  console.log('waitMeasureTableList', waitMeasureTableList);
  return (
    <View className={styles.container}>
      {/* 待测评提示 */}
      <View className={`${styles.waitFinish}`}>
        {/* 显示待测评列表以及数量 */}
        {waitSuccessEvaluation !== 0 && (
          <View className={styles.waitFinishHeader}>
            {/* 展示左侧的文本的提示图表 */}
            <View>
              <Text>待测评</Text>
              <View>
                <Text>{waitSuccessEvaluation}</Text>
              </View>
            </View>
          </View>
        )}
        {/* 展示滚动的nearing */}
        {waitSuccessEvaluation !== 0 && (
          <View className={styles.waitTextItemWapper}>
            {waitMeasureTableList.map((item: any, index: number) => {
              return (
                <WaitTextItem
                  key={String(`${item.measureTableId}${index}`)}
                  updateSetUserData={updateSetUserData}
                  {...item}
                />
              );
            })}
          </View>
        )}
      </View>
      {/* 心理宣教知识内容 */}
      {articleList.length !== 0 && (
        <View className={styles.header}>
          <Text>心理宣教知识</Text>
          {articleList.length === 3 && (
            <View
              onClick={() => {
                Taro.switchTab({
                  url: '/pages/PatientEducation/index',
                });
              }}
            >
              <Text>查看更多</Text>
              <AtIcon prefixClass="icon" value="arrow-right-circle" color="#866DD8" size="18" />
            </View>
          )}
        </View>
      )}
      {/* 展示宣教的内容 */}
      {articleList.map((item: any) => {
        return (
          <View key={item.articleId} className={styles.articleItem}>
            <RenderItemComponent {...item} isHomeScroll extendWapperClass={styles.contentItem} />
          </View>
        );
      })}
      {/* 头部展示标题和更多 */}
      <View className={styles.header}>
        <Text>推荐测评</Text>
        <View
          onClick={() => {
            Taro.switchTab({
              url: '/pages/Evaluation/index',
            });
          }}
        >
          <Text>查看更多</Text>
          <AtIcon prefixClass="icon" value="arrow-right-circle" color="#866DD8" size="18" />
        </View>
      </View>
      <View className={styles.evaluationItemWapper}>
        {/* 主图 */}
        {measureTableList.length !== 0 && (
          <EvaluationItem {...measureTableList[0]} extendWapperClass={styles.extendItemClass} />
        )}
        {/* 两张副图 */}
        {measureTableList.length > 1 && (
          <View className={styles.extendWapper}>
            {measureTableList[1] && (
              <EvaluationItem
                {...measureTableList[1]}
                size={SIZE_TYPE.SMALL}
                extendWapperClass={styles.extendItemClass}
              />
            )}
            {measureTableList[2] && (
              <EvaluationItem
                {...measureTableList[2]}
                size={SIZE_TYPE.SMALL}
                extendWapperClass={styles.extendItemClass}
              />
            )}
          </View>
        )}
      </View>
    </View>
  );
};

export default RecommandTest;
